package genericdemo;

//写一个泛型类，类中有个方法，求数组的最大值
//泛型的上界 使用extends

/**
 * 只要实现了这个接口的都可以接受
 * @param <T>
 */
class Alg<T extends Comparable<T>>{
    public T findMax(T[] array){
        T max = array[0];
        for(int i = 0; i < array.length; i++){
            //为啥报错？
            //引用类型不可以直接用大于小于比较 所以用compareTo
            if(max.compareTo(array[i]) < 0){
                max = array[i];
            }
        }
        return max;
    }
}

class Student implements Comparable<Student>{

    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }
}

class Alg2{
    //没时间传参，静态方法直接类名直接调用 所以我们直接写上<T>
    //并且可以把class Alg2<T> {} 改为 Alg2 {}
    public static<T extends Comparable<T>> T findMax(T[] array){
        T max = array[0];
        for(int i = 0; i < array.length; i++){
            //compareTo出现报错 需要指定边界的地方拷贝到static后面去
            //extends Comparable<T>
            if(max.compareTo(array[i]) < 0){
                max = array[i];
            }
        }
        return max;
    }
}

//泛型的边界
class Test<E extends Number>{

}

public class TestDemo2 {
    public static void main(String[] args) {
        Integer[] array = {1,4,5,21,8,19};
        Integer max = Alg2.findMax(array);
        System.out.println(max);
    }

    public static void main2(String[] args) {
        Test<Number> test4 = new Test<>();
        Test<Integer> test = new Test<>();
        Test<Float> test1 = new Test<>();
        Test<Double> test3 = new Test<>();
    }

    public static void main1(String[] args) {
        Alg<Integer> alg = new Alg<>();
        Integer[] array = {1,4,5,21,8,19};
        Integer max = alg.findMax(array);
        System.out.println(max);

        Alg<Student> alg2 = new Alg<>();
        Student[] students = new Student[3];
        students[0] = new Student("bit", 15);
        students[1] = new Student("hello", 25);
        students[2] = new Student("gaobo", 15);

        Student student = alg2.findMax(students);
        System.out.println(student);
    }
}
